<template>
    <addEditBaseDepartmentView ref="addEditBaseDepartmentView" />
    <addFileView ref="addFileView" />
    <el-main>
        <div class="teacher">
            <div class="app-table">
                <div class="app-body">
                    <div class="app-search">
                        <el-form :inline="true" class="demo-form-inline" v-model="form">
                            <el-form-item label="名称">
                                <el-input placeholder="请输入名称" v-model="form.name"></el-input>
                            </el-form-item>
                            <!-- <el-form-item label="单位名称">
                                            <el-input
                                                    placeholder="请输入单位名称"
                                                    v-model="form.campusName"
                                            ></el-input>
                                        </el-form-item> -->
                            <el-form-item>
                                <el-button type="primary" plain @click="onSubmit" @keyup.enter="searchEnter">搜索
                                </el-button>
                                <el-button type="success" plain @click="add">添加维度</el-button>
                                <!-- <el-button type="warning" @click="exportExcel">导出报表</el-button> -->
                                <!-- <el-button type="danger" @click="importExcel">批量导入<el-icon class="el-icon--right"><Upload /></el-icon></el-button> -->
                            </el-form-item>
                        </el-form>
                    </div>
                    <div style="display:flex">
                        <div style="flex:1;    border: 1px solid #ebeef5;padding-top: 10px;margin-right: 10px;">
                            <el-tree :render-content="renderContent" show-checkbox :data="data" :props="defaultProps"
                                :expand-on-click-node="false" node-key="id" :default-expanded-keys="expanded"
                                ref="tree">
                                <template #default="{ node, data }">
                                    <div class="custom-tree-node" style="display:flex">
                                        <div style="flex:1;">{{ node.label }}</div>
                                        <div style="flex:1" v-if="data.level == 1">
                                            <el-button type="success" style=" margin-left: 20px;" plain size="small"
                                                @click="addNode(node, data)">添加指标</el-button>
                                            <el-button type="primary" style=" margin-left: 10px;" plain size="small"
                                                @click="editNode(node, data)"> 编辑</el-button>
                                            <el-button type="warning" style=" margin-left: 10px;" plain size="small"
                                                @click="deleteBaseDepartment(data.id)"> 删除 </el-button>

                                        </div>
                                        <div style="flex:1" v-if="data.level == 2">
                                            <el-button type="success" style=" margin-left: 20px;" plain size="small"
                                                @click="addNode2(node, data)">添加评价点</el-button>
                                            <el-button type="primary" style=" margin-left: 10px;" plain size="small"
                                                @click="editNode2(node, data)"> 编辑</el-button>
                                            <el-button type="warning" style=" margin-left: 10px;" plain size="small"
                                                @click="deleteBaseDepartment(data.id)"> 删除 </el-button>

                                        </div>
                                        <div style="flex:1" v-if="data.level == 3">
                                            <el-button type="primary" style=" margin-left: 10px;" plain size="small"
                                                @click="editNode3(node, data)"> 编辑</el-button>
                                            <el-button type="warning" style=" margin-left: 10px;" plain size="small"
                                                @click="deleteBaseDepartment(data.id)"> 删除 </el-button>

                                        </div>
                                    </div>
                                </template>
                            </el-tree>
                        </div>
                        <!-- <div style="flex:1">
                            <el-table size="small" :data="list" style="width: 100%" border
                                :row-class-name="tableRowClassName">
                                <el-table-column prop="id" label="id" />
                                <el-table-column prop="levelName" label="名称" />
                                <el-table-column prop="campus" label="配分" />
                                <el-table-column prop="sortNum" label="权重" />
                                <template v-slot:empty>
                                    <el-empty description="暂无数据"></el-empty>
                                </template>
                            </el-table>
                            <el-config-provider :locale="locale">
                                <el-pagination :total="total" :page-size="limit"
                                    :page-sizes="[5, 10, 20, 30, 40, 50, 100]"
                                    layout="total, sizes, prev, pager, next, jumper" @size-change="changePageSize"
                                    @current-change="changeCurrentPage">
                                </el-pagination>
                            </el-config-provider>
                        </div> -->
                    </div>
                </div>
                <bottomView />
            </div>
        </div>
    </el-main>
</template>
<script>
import addEditBaseDepartmentView from "@/components/evaluate/addEditBaseQuestionBank.vue";
import addFileView from "@/components/addExcelFile.vue";
import { ElConfigProvider, ElMessageBox, ElMessage } from "element-plus";
import zhCn from "element-plus/lib/locale/lang/zh-cn";
import { httpService } from "@/utils/httpService";
import { Picture as IconPicture, Upload } from "@element-plus/icons-vue";
import bottomView from "@/components/bottom.vue";

export default {
    name: "BaseDepartmentListView",
    data() {
        return {
            type: 0,
            title: "",
            page: 0,
            pageSize: 10,
            total: 0,
            limit: 10,
            expanded: [],
            list: [],
            form: {
                name: "",
                campusName: "",
            },
            data: [

            ],
            defaultProps: {
                children: 'children',
                label: 'label',
            },
        };
    },
    components: {
        addEditBaseDepartmentView,
        addFileView,
        IconPicture,
        ElConfigProvider,
        ElMessageBox,
        ElMessage, Upload, bottomView,
    },
    setup() {
        return {
            locale: zhCn,
        };
    },
    mounted() {
        this.getBaseDepartmentList();
    },
    watch: {
        $route() {
            this.getBaseDepartmentList();
        },
    },
    methods: {
        handleNodeClick(data) {
            console.log(data)
        },
        searchEnter: function (e) {
            // 使用 which 和 keyCode 属性来解决兼容问题
            var keyCode = window.event ? e.keyCode : e.which;
            var val = e.target.value;
            if (keyCode == 13 && val) {
                this.getBaseDepartmentList();
            }
        },
        add() {
            this.$refs.addEditBaseDepartmentView.title = "添加类别"
            this.$refs.addEditBaseDepartmentView.type = 1
            this.$refs.addEditBaseDepartmentView.dialogFormVisible = true;
        },
        editBaseDepartment(obj) {
            this.expanded.push(obj.id)
            this.$refs.addEditBaseDepartmentView.title = "修改类别"
            this.$refs.addEditBaseDepartmentView.type = 1
            this.$refs.addEditBaseDepartmentView.getBaseDepartmentById(obj.id);
            this.$refs.addEditBaseDepartmentView.dialogFormVisible = true;
        },
        addNode(node, data) {
            //    console.log(node)
            console.log(data.id)
            this.expanded.push(data.id)
            this.$refs.addEditBaseDepartmentView.title = "添加项目"
            this.$refs.addEditBaseDepartmentView.addBaseDepartmentForm.parentId = data.id
            this.$refs.addEditBaseDepartmentView.type = 2
            this.$refs.addEditBaseDepartmentView.dialogFormVisible = true;
        },
        addNode2(node, data) {
            //    console.log(node)
            console.log(data.id)

            this.expanded.push(data.id)
            this.$refs.addEditBaseDepartmentView.title = "添加评价点"
            this.$refs.addEditBaseDepartmentView.addBaseDepartmentForm.parentId = data.id
            this.$refs.addEditBaseDepartmentView.type = 3
            this.$refs.addEditBaseDepartmentView.dialogFormVisible = true;
        },
        editNode(node, data) {
            this.expanded.push(data.id)

            //    console.log(node)
            this.$refs.addEditBaseDepartmentView.title = "修改类别"
            this.$refs.addEditBaseDepartmentView.type = 1
            this.$refs.addEditBaseDepartmentView.getBaseDepartmentById(data.id);
            this.$refs.addEditBaseDepartmentView.dialogFormVisible = true;
        },
        editNode2(node, data) {
            this.expanded.push(data.id)

            //    console.log(node)
            this.$refs.addEditBaseDepartmentView.title = "修改项目"
            this.$refs.addEditBaseDepartmentView.type = 2
            this.$refs.addEditBaseDepartmentView.getBaseDepartmentById(data.id);
            this.$refs.addEditBaseDepartmentView.dialogFormVisible = true;
        },
        editNode3(node, data) {
            this.expanded.push(data.id)
            //    console.log(node)
            this.$refs.addEditBaseDepartmentView.title = "修改评价点"
            this.$refs.addEditBaseDepartmentView.type = 3
            this.$refs.addEditBaseDepartmentView.getBaseDepartmentById(data.id);
            this.$refs.addEditBaseDepartmentView.dialogFormVisible = true;
        },
        deleteBaseDepartment(id) {
            ElMessageBox.confirm("确定要删除本条记录吗？", "提醒", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    httpService("/javaApi/baseQuestionBank/deleteById", "id=" + id, "post").then(
                        (data) => {
                            if (data.code == 200) {
                                ElMessage.success("删除成功");
                                this.getBaseDepartmentList();
                            } else {
                                // ElMessage.error("删除失败，请重试！");
                            }
                        }
                    );
                })
                .catch(() => {
                });
        },
        reloadData() {
            this.page = 1;
            this.getBaseDepartmentList()
        },
        importExcel() {
            this.$refs.addFileView.dialogFormVisible = true;
            this.$refs.addFileView.title = "批量导入部门表";
            this.$refs.addFileView.uploadApiUrl = "/javaApi/baseQuestionBank/importExcel";
        },
        exportExcel() {
            httpService(
                "/javaApi/baseQuestionBank/exportExcel",
                {
                    "name": this.form.name,
                    "campusName": this.form.campusName,
                },
                "get"
            ).then((data) => {
                if (data.code == 200) {
                    window.open(data.msg, '_blank')
                }
            });
        },
        onSubmit() {
            this.getBaseDepartmentList();
        },
        // 每页记录数改变，size：回调参数，表示当前选中的“每页条数”
        changePageSize(size) {
            this.pageSize = size;
            this.limit = size;
            this.getBaseDepartmentList();
        },

        // 改变页码，page：回调参数，表示当前选中的“页码”
        changeCurrentPage(page) {
            this.page = page;
            this.getBaseDepartmentList();
        },
        tableRowClassName({ row, rowIndex }) {
            return "";
        },
        getBaseDepartmentList() {
            this.getTreeData()
            httpService(
                "/javaApi/baseQuestionBank/list/" + this.page + "/" + this.pageSize,
                {
                    "name": this.form.name,
                    "campusName": this.form.campusName,
                },
                "get"
            ).then((data) => {
                this.list = data.list;
                this.total = data.total;
                this.pageCount = data.pages;
            });
        },
        getTreeData() {

            httpService(
                "/javaApi/baseQuestionBank/queryTree",
                {
                    "name": this.form.name,
                    "campusName": this.form.campusName,
                },
                "get"
            ).then((data) => {
                this.data = data;
                console.log(this.expanded)

                this.expanded.push(data[0].id)
                console.log(this.expanded)

            });
        },
    },
};
</script>
<style scoped>
.el-main {
    padding: 0px;
}

.teacher-content {
    width: 100%;
}

.app-table {
    background: #eff3f5;
    padding: 10px;
}

.app-body {
    background: white;
    padding: 20px 20px 10px 20px;
    margin-bottom: 10px;
    min-height: calc(100vh - 243px);
}

.app-search {
    padding-top: 10px;
    margin-top: 10px;
}

.app-separator {
    padding-top: 10px;
    padding-bottom: 20px;
    padding-left: 10px;
}

.el-table .warning-row {
    --el-table-tr-bg-color: var(--el-color-warning-lighter);
}

.el-table .success-row {
    --el-table-tr-bg-color: var(--el-color-success-lighter);
}

.el-pagination {
    text-align: center;
    margin-top: 10px;
}
</style>
